package com.yupi.usercenter.base.model;

import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class AntiSQLFilter {
    private static final String[] KEY_WORDS = new String[]{";", "\"", "'", "/*", "*/", "--", "exec", "select", "update", "delete", "insert", "alter", "drop", "create", "shutdown"};

    public static Map<String, String[]> getSafeParameterMap(Map<String, String[]> parameterMap) {
        MapsUtils.MapBuilder<String, String[]> builder = MapsUtils.builder(HashMap::new);
        Iterator iter = parameterMap.keySet().iterator();

        while (iter.hasNext()) {
            String key = (String) iter.next();
            String[] oldValues = (String[]) parameterMap.get(key);
            builder.put(key, getSafeValues(oldValues));
        }

        return builder.unmodifiable().build();
    }

    public static String[] getSafeValues(String[] oldValues) {
        if (!ArrayUtils.isNotEmpty(oldValues)) {
            return null;
        } else {
            String[] newValues = new String[oldValues.length];

            for (int i = 0; i < oldValues.length; ++i) {
                newValues[i] = getSafeValue(oldValues[i]);
            }

            return newValues;
        }
    }

    public static String getSafeValue(String oldValue) {
        StringBuilder sb = new StringBuilder(oldValue);
        String lowerCase = oldValue.toLowerCase();
        String[] var3 = KEY_WORDS;
        int var4 = var3.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            String keyWord = var3[var5];

            int x;
            while ((x = lowerCase.indexOf(keyWord)) >= 0) {
                if (keyWord.length() == 1) {
                    sb.replace(x, x + 1, " ");
                    lowerCase = sb.toString().toLowerCase();
                } else {
                    sb.deleteCharAt(x + 1);
                    lowerCase = sb.toString().toLowerCase();
                }
            }
        }

        return sb.toString();
    }
}